<?php

/**
 * 刷题操作后续操作：不需要即时的操作一律放到队列
 */

namespace App\Jobs;

use App\Models\Qh_Question;
use App\Models\User;
use App\Service\Redis\RedisBaseService;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use PHPUnit\Exception;

class UserLearnQuestionHelperJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected int $questionId;
    protected User $user;
    protected array $params;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($questionId, $user, $params=[])
    {
        $this->questionId = $questionId;
        $this->user = $user;
        $this->params = $params;
    }

    /**
     * Execute the job.
     *
     * @param RedisBaseService $redisBaseService
     * @return void
     * @throws \RedisException
     */
    public function handle(RedisBaseService $redisBaseService)
    {
        try {
            $question = Qh_Question::find($this->questionId);
            if ($question) {
                $exits = DB::table('qh_user_learn_question_logs')->where('userId', $this->user->id)->where('questionId', $this->questionId)->first();
                if (!$exits) {
                    DB::table('qh_user_learn_question_logs')->insert([
                        'userId' => $this->user->id,
                        'qlId' => $question->qlId,
                        'courseId' => $question->courseId,
                        'knowledgeId' => $question->knowledgeId,
                        'questionId' => $question->id,
                        'created_at' => Carbon::now(),
                        'updated_at' => Carbon::now()
                    ]);
                }

                //// 刷题数量更新
                //$allCount = $redisBaseService->getUserStatisticsData('statistics:userLearnQuestionCount', $this->user->id)??$this->user->learnQuestionCount;
                //$todayCount = 1;
                //
                //if ($this->user->learnQuestionOfLastTime == null || !Carbon::parse($this->user->learnQuestionOfLastTime)->isToday()) {
                //    $this->user->learnQuestionOfLastTime = Carbon::now();
                //}else {
                //    $todayCount = $redisBaseService->getUserStatisticsData('statistics:userLearnQuestionOfTodayCount', $this->user->id)??$this->user->learnQuestionCountOfToday;
                //}
                //
                //$this->user->learnQuestionCount = $allCount;
                //$this->user->learnQuestionCountOfToday = $todayCount;
                //$this->user->save();

                // 更新刷题次数到缓存中
                // $redisBaseService->updateUserLearnQuestionCount($this->user->id, $this->user->learnQuestionCount, $this->user->learnQuestionCountOfToday);
            }
        }catch (Exception $e) {
            \Log::error('[刷题助手]处理异常', [
                'userId' => $this->user->id,
                'questionId' => $this->questionId,
                'params' => $this->params
            ]);
        }

        \Log::debug('[刷题助手]处理完成', [
            'userId' => $this->user->id,
            'questionId' => $this->questionId
        ]);
    }
}
